package cn.com.zhbs.account.service.impl;

import cn.com.zhbs.account.dao.AccountsMapper;
import cn.com.zhbs.account.dao.DepartmentMapper;
import cn.com.zhbs.account.entitys.Department;
import cn.com.zhbs.account.service.DepartmentService;
import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.com.zhbs.account.util.Util.sparatePage;

/**
 * @program: vr_manage
 * @description: 机构实现层
 * @author: 陈治杰
 * @create: 2021-08-07 14:29
 **/
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    DepartmentMapper departmentMapper;
    @Resource
    AccountsMapper accountsMapper;

    @Override
    public Map<String, Object> addDepartment(JSONObject jsonObject) {
        Integer isExist=departmentMapper.isDepartExist(jsonObject);
        if (isExist == 0) {
            //添加主机构
            jsonObject.put(Department.parent_id.toString(),0);
            departmentMapper.insertDepart(jsonObject);
            //将用户添加到这个机构
            accountsMapper.insertDepartment(jsonObject);
            //添加子机构
            List<JSONObject> children = JSONArray.parseArray(jsonObject.getJSONArray("children").toJSONString(), JSONObject.class);
            for(JSONObject child:children){
                child.put(Department.parent_id.toString(),jsonObject.getString(Department.did.toString()));
                departmentMapper.insertDepart(child);
            }
            return ResultFactory.success().display();
        }else{
           return ResultFactory.failure("0","该机构已存在",null).display();
        }
    }

    @Override
    public Map<String, Object> departmentList(JSONObject jsonObject) {
        Map<String,Object> result=new HashMap<>();
        Integer currentPage=Integer.parseInt(jsonObject.getString("currentPage"));
        Integer pageSize=Integer.parseInt(jsonObject.getString("pageSize"));
        Integer start=(currentPage-1)*pageSize;
        jsonObject.put("start",start);
        jsonObject.put("pageSize",pageSize);
        jsonObject.put("dids",departmentMapper.findDIDByAID(jsonObject));
        List<JSONObject> departments=departmentMapper.findDepartmentByAID(jsonObject);
        List<Map<String,Object>> resultList=new ArrayList<>();
        List<String> removeList=new ArrayList<>();
        for(JSONObject dapartment:departments){
            JSONObject json=new JSONObject();
            json.putAll(dapartment);
            if(dapartment.get(Department.parent_id.toString()).toString().equals("0")){
                List<JSONObject> children=new ArrayList<>();
                for(int i=0;i<departments.size();i++){
                    if(departments.get(i).get(Department.parent_id.toString()).toString()
                            .equals(dapartment.get(Department.did.toString()).toString())){
                        children.add(departments.get(i));
                        removeList.add(departments.get(i).get(Department.did.toString()).toString());
                    }
                }
                json.put("children",children);
            }
            resultList.add(json);
        }
        for(int j=0;j<resultList.size();j++){
            for(int i=0;i<removeList.size();i++){
                if(resultList.get(j).get(Department.did.toString()).toString().equals(removeList.get(i))){
                    resultList.remove(j);
                    j--;
                    break;
                }
            }
        }
        result.put("total",resultList.size());
        result.put("list",sparatePage(resultList,Integer.parseInt(jsonObject.get("start").toString()),Integer.parseInt(jsonObject.get("pageSize").toString())));
        return result;
    }

    @Override
    public Map<String, Object> editDepartment(JSONObject jsonObject) {
        Integer isExist=departmentMapper.isDepartExist(jsonObject);
        if (isExist == 0) {
            //修改主机构
            departmentMapper.updateDepartment(jsonObject);
            //添加子机构
            List<Integer> deleteChildrenList=new ArrayList<>();
            deleteChildrenList.add(0);
            List<JSONObject> children = JSONArray.parseArray(jsonObject.getJSONArray("children").toJSONString(), JSONObject.class);
            for(JSONObject child:children){
                if(!child.getString(Department.did.toString()).equals("")){
                    departmentMapper.updateDepartment(child);
                }else{
                    child.put(Department.parent_id.toString(), jsonObject.get(Department.did.toString()));
                    departmentMapper.insertDepart(child);
                }
                deleteChildrenList.add(child.getInteger(Department.did.toString()));
            }
            jsonObject.put("children",deleteChildrenList);
            departmentMapper.deleteChildByParentID(jsonObject);
            return ResultFactory.success().display();
        }else{
            return ResultFactory.failure("0","该机构已存在",null).display();
        }
    }

    @Override
    public Map<String, Object> departmentDetail(JSONObject jsonObject) {
        JSONObject result=departmentMapper.findDepartmentByDID(jsonObject);
        result.put("children",departmentMapper.findDepartmentByParentID(jsonObject));
        return result;
    }

    @Override
    public void deleteDepartment(JSONObject jsonObject) {
        departmentMapper.deleteDepartment(jsonObject);
    }
}
